﻿/**
 * \file   lv_MyCallabck.c
 * \brief
 * https://lvgl-chinese-doc.readthedocs.io/zh-cn/latest/overview/event.html#add-events-to-the-object
 * \author wangxiaohang
 * \date   December 2023
 *********************************************************************/
#include "lv_Agriculture.h"
#include "stdio.h"
/**
 * 键盘回调函数.
 *
 * \param e
 */
void KeyBorad_Callback(lv_event_t *e)
{
    lv_event_code_t code = lv_event_get_code(e);
    lv_obj_t *ta = lv_event_get_target(e);
    lv_obj_t *kb = lv_event_get_user_data(e);

    if (code == LV_EVENT_PRESSED)
    {
        lv_keyboard_set_textarea(kb, ta);
        lv_obj_set_style_max_height(kb, LV_HOR_RES * 2 / 3, 0);
        lv_obj_clear_flag(kb, LV_OBJ_FLAG_HIDDEN);
        lv_group_focus_obj(kb);
        lv_group_set_editing(lv_obj_get_group(kb), kb);
        lv_obj_scroll_to_view_recursive(ta, LV_ANIM_OFF);
        lv_indev_wait_release(lv_event_get_param(e));
        printf("keyboard callback\n");
    }
    if (code == LV_EVENT_CANCEL)
    {
        lv_obj_add_flag(kb, LV_OBJ_FLAG_HIDDEN);
        lv_indev_reset(NULL, ta); /*To forget the last clicked object to make it focusable again*/

        printf("keyboard \n");
    }
}

/**
 * lv_MqttSend.c中的发送按钮回调
 *
 * \param e
 */
void SendBtn_Callback(lv_event_t *e)
{
    lv_event_code_t code = lv_event_get_code(e);
    lv_obj_t *ta = lv_event_get_target(e);
    lv_obj_t *kb = lv_event_get_user_data(e);

    printf("SendBtn_Callback:This is test statement!\r\n");
}

/**
 * lv_setting.c中设置列表回调.
 *
 * \param e
 */
void List_Callback(lv_event_t *e)
{
    lv_event_code_t code = lv_event_get_code(e);
    lv_obj_t *ta = lv_event_get_target(e);
    lv_obj_t *kb = lv_event_get_user_data(e);
    if (code == LV_EVENT_PRESSED)
    {
        lv_WIFIList_Page();
        lv_scr_load_anim(lv_WIFIList_Srceen, LV_SCR_LOAD_ANIM_FADE_ON, 500, 0, false);
    }
}

/**
 * lv_WIFIConnect.c wifi列表回调.
 *
 * \param e
 */
void WIFI_Callback(lv_event_t *e)
{
    lv_event_code_t code = lv_event_get_code(e);
    lv_obj_t *ta = lv_event_get_target(e);
    lv_obj_t *kb = lv_event_get_user_data(e);
    if (code == LV_EVENT_PRESSED)
    {
        lv_obj_set_style_bg_opa(lv_layer_top(), LV_OPA_50, 0);
        lv_obj_set_style_bg_color(lv_layer_top(), lv_palette_main(LV_PALETTE_GREY), 0);
        lv_WIFI_Connect(lv_layer_top(), kb);
    }
}

/**
 * wifi连接按钮回调.
 *
 * \param e
 */
void WIFI_ConnectBtn_Callback(lv_event_t *e)
{
    lv_event_code_t code = lv_event_get_code(e);
    lv_obj_t *ta = lv_event_get_target(e);
    lv_obj_t *kb = lv_event_get_user_data(e);

    printf("WIFI_ConnectBtn:This is test statement!\r\n");
}
/**
 * 取消连接按钮.
 *
 * \param e
 */
void WIFI_ExitBtn_Callback(lv_event_t *e)
{
    lv_event_code_t code = lv_event_get_code(e);
    lv_obj_t *ta = lv_event_get_target(e);
    lv_obj_t *kb = lv_event_get_user_data(e);

    if (code == LV_EVENT_PRESSED)
    {
        lv_obj_clear_flag(lv_layer_top(), LV_OBJ_FLAG_CLICKABLE);
        lv_obj_set_style_bg_opa(lv_layer_top(), LV_OPA_TRANSP, 0);
        lv_obj_del(WIFIConnect);
        lv_indev_set_group(indev_keypad, wifi_list_group);
    }
    printf("WIFI_ExitBtn:This is test statement!\r\n");
}
